﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sec;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.WinUI.Sec
{
    public partial class frmManageRole : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private SecRoleEntity mRoleEntity;
        #endregion

        #region Constructor
        public frmManageRole()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colRoleCategory, typeof(RoleCategory), typeof(Decimal), true);
        }

        private void RefreshDataGrid()
        {
            DataSet ds = new DataSet();
            ds = SecRoleBs.GetDsAllRoles();
            if (ds != null && ds.Tables.Count > 0)
            {
                gvMain.DataSource = ds.Tables[0];
            }
            else
            {
                gvMain.DataSource = null;
            }
        }

        #endregion

        #region Event
        private void frmManageRole_Load(object sender, EventArgs e)
        {
            BindComboBox();
            RefreshDataGrid();
        }

        protected override void Click_View()
        {
            if (mRoleEntity == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainRole frm = new frmMaintainRole();
            frm.ThisActionType = ActionType.View;
            frm.RoleEntity = mRoleEntity;
            frm.ShowDialog();
        }

        protected override void  Click_Delete()
        {
            if (mRoleEntity == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确定删除？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = SecRoleBs.DeleteSecRole(mRoleEntity);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    RefreshDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void  Click_Modify()
        {
            if (mRoleEntity == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainRole frm = new frmMaintainRole();
            frm.ThisActionType = ActionType.Modify;
            frm.RoleEntity = mRoleEntity;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                RefreshDataGrid();
            }
        }

        protected override void  Click_New()
        {
            frmMaintainRole frm = new frmMaintainRole();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                RefreshDataGrid();
            }
        }

        protected override void Click_Submit()
        {
            if (mRoleEntity == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainRoleSetting frm = new frmMaintainRoleSetting();
            frm.RoleEntity = mRoleEntity;
            frm.ShowDialog();
        }
        #endregion

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            mRoleEntity = new SecRoleEntity(gridView1.GetDataRow(e.RowHandle));
        }
    }
}
